import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useGlobalStore = defineStore('global', () => {
    const globalDefine = {
      token: '',
      theme: false,
      userInfo: {
        id: null,
        avatarUrl: null,
        lastTime: null,
        nickName: null,
        roles: [] as any[]
      },
      caches: {} as any
    }
    const global = ref(globalDefine)


    //重置
    const reset = () => {
      global.value = globalDefine
    }

    //将信息复制到对象中
    const login = (data: any) => {
      global.value.userInfo = data.userInfo
      global.value.token = data.token
    }

    //设置缓存
    const setCache = (caches: any) => {
      global.value.caches = caches
    }

    return { global, login, reset,setCache }
  }, {
    persist: {
      key: 'global',
      storage: localStorage
    }
  }
)
